package CtrLayer;
import java.util.ArrayList;

import DBLayer.DBStock;
import DBLayer.DbConnection1;
import DBLayer.IFDBStock;
import ModelLayer.Stock;

public class StockCtr {
	
private static StockCtr instance;
	
	private StockCtr()
	{
		
	}
	
	public static StockCtr getInstance()
    {
        if(instance==null)
        {
            instance = new StockCtr();
        }
        return instance;
    }
	
	public ArrayList<Stock> findAllStocks()
	{
		IFDBStock dbS = new DBStock();
	    ArrayList<Stock> allR = new ArrayList<Stock>();
	    allR = dbS.getAllStocks(false);
	    return allR;
	}
	
	public Stock findByName(String stockName)
    {
        IFDBStock dbS = new DBStock();
        return dbS.searchStockName(stockName);
    }
	
	public Stock findByStockId(int stockId)
    {
        IFDBStock dbS = new DBStock();
        return dbS.findStock(stockId);
    }
	
	public int deleteByStockId(int stockId)
    {
        IFDBStock dbS = new DBStock();
        return dbS.deleteStock(stockId);
    }
	
	public int updateStock(int stockId, String stockName)
    {
        IFDBStock dbS = new DBStock();
        Stock r = new Stock();
        r.setLocationId(stockId);
        r.setLocationName(stockName);
        return  dbS.updateStock(r);
    }
	public void createStock(String stockName)
    {    
         Stock sObj = new Stock();
         sObj.setLocationName(stockName);
         
         try{
          DbConnection1.startTransaction();
          DBStock dbS = new DBStock();
          dbS.insertStock(sObj);
          DbConnection1.commitTransaction();
         }
         catch(Exception e)
         {
             DbConnection1.rollbackTransaction();
         }
    }
	


}
